﻿package code {
	import flash.display.Shape;
	// represents a line derived from schwartz code
	public class Line extends Shape {
		protected var thickness:Number;  // thickness of the line
		public var a:Vertex;  // startpoint
		public var b:Vertex;  // endpoint
		protected var color:uint;  // color of the line
		public var startDist:Number;
		public var normal:Vertex;
		public var pointD:Number;
		
		public function Line(a:Vertex, b:Vertex, thickness:Number, color:uint) {
			this.a = a;
			this.b = b;
			this.thickness = thickness;
			this.color = color;
			if(a != null && b != null)
				init();
			//startDist = distance();
		}
		
		// generates the line
		public function init() {
			graphics.clear();
			graphics.lineStyle(thickness, 0x0000FF);
			graphics.moveTo(a.x, a.y);
			graphics.lineTo(b.x, b.y);
		}
		
		public function update(a:Vertex, b:Vertex):void {
			this.a = a;
			this.b = b;
			graphics.clear();
			init();
		}
		
		// gets theta in radians:
		public function getAngle():Number {
			var xLength:Number = b.x - a.x;
			var yLength:Number = b.y - a.y;
			return Math.atan2(yLength, xLength);
		}
		
		// distance:
		public function distance():Number {
			var xLength:Number = b.x - a.x;
			var yLength:Number = b.y - a.y;
			return Math.sqrt(xLength*xLength + yLength*yLength);
		}
		
		// gets normal vector
		public function getNormal():Vertex{
			/*var crossProduct:Vertex = Vertex.cross(a, b);  // first get the cross product of the plane, this will let you get n, the normal
			var aMagnitude:Number = Vertex.getMagnitude(a); // |a| 
			var bMagnitude:Number = Vertex.getMagnitude(b); // |b|
			var theta:Number = Vertex.getAngle(a, b); 
			var sin:Number = Math.sin(theta); 
			var normalVertex:Vertex = Vertex.divide(aMagnitude * bMagnitude * sin, crossProduct); // n = (a x b) / (|a||b|sin(theta))
			normal = normalVertex;*/
			/*var slope = (b.y - a.y) / (b.x - a.x);
			var perpSlope = 1 / slope;
			var midPoint:Vertex = new Vertex((a.x + b.x) / 2, (a.y + b.y) / 2);
			var b:Number = midPoint.y - (perpSlope * midPoint.x);
			var normalVector = new Vertex(perpSlope / b, 1 / b);*/
			var normalVertex:Vertex = new Vertex(-(b.y - a.y), (b.x - a.x));
			//trace("Normal Vertex: " + normalVertex.x, normalVertex.y);
			//trace("Normal vertex: " + normalVertex.x, normalVertex.y);
			var normalUnitVector:Vertex = Vertex.divide(Vertex.getMagnitude(normalVertex), normalVertex);
			//trace("Normalized: " + normalUnitVector.x, normalUnitVector.y);
			normal = normalUnitVector;
			return normalUnitVector;
		}
		
		// gets point a on the plane
		public function getPoint():Number{
			var testPointX:Number = a.x + (2/3)*(b.x - a.x);
			var testPointY:Number = a.y + (2/3)*(b.y - b.y);
			pointD = Vertex.dot(normal, a);
			return pointD;
		}
		
	}
}